/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#include "VideoPlayerState.h"
#include "VideoPlayerFunctions.h"
#include "ScriptManager.h"
#include "InputManager.h"
#include "StateManager.h"

template<> VideoPlayerState* Singleton<VideoPlayerState>::ms_Singleton =
		0;

VideoPlayerState::VideoPlayerState(QObject* rParent) :
	QObject(rParent) {
	mVideoPlayer = 0;
	mPlayStatus = false;
}

VideoPlayerState* VideoPlayerState::getSingletonPtr(void) {
	return ms_Singleton;
}

VideoPlayerState& VideoPlayerState::getSingleton(void) {
	assert( ms_Singleton );
	return (*ms_Singleton);
}

void VideoPlayerState::cleanup() {
	if (mPlayStatus) {
		mPlayList.clear();
		stopCurrentVideo(false);
	}
}

void VideoPlayerState::update() {

	if (mPaused)
		return;

	if (mPlayStatus) {
		if (!mVideoPlayer->update()) {
			stopCurrentVideo();
		}
		return;
	}
}

void VideoPlayerState::pause() {
	StateManager::scriptEvent("videoPlayer.onpause();");
	mPaused = true;
}

void VideoPlayerState::init(const QString& rScript) {

	StateManager::scriptEvent("videoPlayer.onload();");

	if ( mPlayList.isEmpty() ) {
		StateManager::scriptEvent("videoPlayer.onexit();");
		return;
	}

	mPaused = false;
	playNextVideo();
}

void VideoPlayerState::resume() {
	StateManager::scriptEvent("videoPlayer.onresume();");
	mPaused = false;
}

void VideoPlayerState::injectKeyPressed(int rKey, unsigned int rText) {
	(void) rKey;
	(void) rText;
}

void VideoPlayerState::injectMousePressed(int rX, int rY, int rMouseButton) {
	(void) rX;
	(void) rY;
	(void) rMouseButton;
}

void VideoPlayerState::injectMouseMoved(int rX, int rY, int rZ) {
	(void) rX;
	(void) rY;
	(void) rZ;
}

void VideoPlayerState::injectKeyReleased(int rKey) {
	switch (rKey) {
	case InputManager::Escape:
		stopCurrentVideo();
		break;
	default:
		break;
	}
}

void VideoPlayerState::injectMouseReleased(int rX, int rY, int rMouseButton) {
	(void) rX; (void) rY; (void) rMouseButton;
}

void VideoPlayerState::addToPlaylist(const QString& rName,
		const QString& rGroupName) {

	QStringList video;
	video.append(rName);
	video.append(rGroupName);
	mPlayList.enqueue(video);
}

void VideoPlayerState::playNextVideo() {

	if (mPlayStatus || mPaused)
		return;

	if (!mVideoPlayer)
		mVideoPlayer = new VideoPlayer(this);

	QStringList nextVideo = mPlayList.dequeue();

	mVideoPlayer->playVideo(OggFilePtr(new OggFile(nextVideo[0], nextVideo[1])));
	mPlayStatus = true;
}

void VideoPlayerState::stopCurrentVideo(bool rEmitSignal) {

	if (!mPlayStatus || mPaused)
		return;

	mVideoPlayer->releaseCurrentVideo();
	mPlayStatus = false;

	if (mPlayList.size() > 0) {
		playNextVideo();
		return;
	}

	if (rEmitSignal) {
		StateManager::scriptEvent("videoPlayer.onexit();");
	}
}

VideoPlayerState::~VideoPlayerState() {
	// TODO Auto-generated destructor stub
}
